package au.com.centrumsystems.hudson.plugin.buildpipeline;

import hudson.model.AbstractProject;

import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;

import java.util.ArrayList;
import java.util.List;

import au.com.centrumsystems.hudson.plugin.util.BuildUtil;

/**
 * @author marcin
 * 
 *         Representation of a set of projects
 * 
 */
public class ProjectForm extends CellForm<ProjectForm> {

    public static class Builder {

        private final ProjectForm.Factory factory;

        private AbstractProject<?, ?> project;

        @Inject
        public Builder(ProjectForm.Factory factory) {
            this.factory = factory;
        }

        public Builder setProject(AbstractProject<?, ?> project) {
            this.project = project;
            return this;
        }

        public ProjectForm build() {
            List<ProjectForm> dependencies = Lists.<ProjectForm>newArrayList();
            for (AbstractProject<?, ?> downstream : project.getDownstreamProjects()) {
                ProjectForm projectForm = new ProjectForm.Builder(factory)
                    .setProject(downstream)
                    .build();
                dependencies.add(projectForm);
            }
            return factory.create(project, dependencies);
        }
    }

    public interface Factory {
        ProjectForm create(AbstractProject<?, ?> project, List<ProjectForm> dependencies);
    }

    private final BuildUtil buildUtil;
    private final AbstractProject<?, ?> project;

    /**
     * @param project
     *            project
     */
    @Inject
    public ProjectForm(@Assisted AbstractProject<?, ?> project, @Assisted List<ProjectForm> dependencies, BuildUtil buildUtil) {
        super(dependencies);
        this.project = project;
        this.buildUtil = buildUtil;
    }

    public String getName() {
        return project.getName();
    }

    public String getHealth() {
        return project.getBuildHealth().getIconUrl().replaceAll("\\.gif", "\\.png");
    }

    public String getResult() {
        return buildUtil.getStatus(project.getLastBuild());
    }

    public String getUrl() {
        return project.getUrl();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((project == null) ? 0 : project.hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ProjectForm other = (ProjectForm) obj;
        if (project == null) {
            if (other.project != null) {
                return false;
            }
        } else if (!project.equals(other.project)) {
            return false;
        }
        return true;
    }
}
